from django.shortcuts import render
from django.views import View
from alipay import AliPay
from django.conf import settings
import os
from django import http
from django.db import transaction

from meiduo_mall.utils.views import LoginRequiredJSONMixin
from orders.models import OrderInfo
from meiduo_mall.utils.response_code import RETCODE
from .models import Payment
from orders.models import OrderInfo


# Create your views here.
class PaymentStatusView(LoginRequiredJSONMixin, View):
    def get(self, request):
        arguments = request.GET
        argument_dict = arguments.dict()
        signature = argument_dict.pop('sign')

        app_private_key_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "keys/app_private_key.pem")
        alipay_public_key_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "keys/alipay_public_key.pem")

        app_private_key_string = open(app_private_key_path).read()
        alipay_public_key_string = open(alipay_public_key_path).read()

        alipay = AliPay(  # 传入公共参数（对接任何接口都要传递的）
            appid=settings.ALIPAY_APPID,  # 应用ID
            app_notify_url=None,  # 默认回调url，如果采用同步通知就不传
            # 应用的私钥和支付宝公钥的路径
            app_private_key_string=app_private_key_string,
            alipay_public_key_string=alipay_public_key_string,
            sign_type="RSA2",  # 加密标准
            debug=settings.ALIPAY_DEBUG  # 指定是否是开发环境
        )

        success = alipay.verify(argument_dict, signature)
        order_id = argument_dict['out_trade_no']
        trade_id = argument_dict['trade_no']

        if success:
            with transaction.atomic():
                save_id = transaction.savepoint()

                try:
                    payInfo = Payment.objects.get(order_id=order_id)
                    return http.HttpResponseForbidden('Illegal request')
                except Payment.DoesNotExist:
                    pass

                try:
                    Payment.objects.create(
                        order_id=order_id,
                        trade_id=trade_id
                    )
                    OrderInfo.objects.filter(order_id=order_id, status=OrderInfo.ORDER_STATUS_ENUM['UNPAID']).update(
                        status=OrderInfo.ORDER_STATUS_ENUM['UNCOMMENT']
                    )

                except Exception as e:
                    transaction.savepoint_rollback(save_id)
                    return http.HttpResponseForbidden('Exception request')

            transaction.savepoint_commit(save_id)

            context = {
                'trade_id': trade_id
            }
            return render(request, 'pay_success.html', context)
        else:
            return http.HttpResponseForbidden('Illegal request')


class PaymentView(LoginRequiredJSONMixin, View):
    """对接支付宝的支付接口"""

    def get(self, request, order_id):
        """
        :param order_id: 当前要支付的订单ID
        :return: JSON
        """
        user = request.user
        # 校验order_id
        try:
            order = OrderInfo.objects.get(order_id=order_id, user=user, status=OrderInfo.ORDER_STATUS_ENUM['UNPAID'])
        except OrderInfo.DoesNotExist:
            return http.HttpResponseForbidden('订单信息错误')

        app_private_key_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "keys/app_private_key.pem")
        alipay_public_key_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "keys/alipay_public_key.pem")

        app_private_key_string = open(app_private_key_path).read()
        alipay_public_key_string = open(alipay_public_key_path).read()

        # 创建对接支付宝接口的SDK对象
        alipay = AliPay( # 传入公共参数（对接任何接口都要传递的）
            appid=settings.ALIPAY_APPID, # 应用ID
            app_notify_url=None,  # 默认回调url，如果采用同步通知就不传
            # 应用的私钥和支付宝公钥的路径
            app_private_key_string=app_private_key_string,
            alipay_public_key_string=alipay_public_key_string,
            sign_type="RSA2", # 加密标准
            debug=settings.ALIPAY_DEBUG # 指定是否是开发环境
        )

        # SDK对象对接支付宝支付的接口，得到登录页的地址
        order_string = alipay.api_alipay_trade_page_pay(
            out_trade_no=order_id, # 订单编号
            total_amount=str(order.total_amount), # 订单支付金额
            subject="美多商城%s" % order_id, # 订单标题
            return_url=settings.ALIPAY_RETURN_URL # 同步通知的回调地址，如果不是同步通知，就不传
        )

        # 拼接完整的支付宝登录页地址
        # 电脑网站支付(正式环境)，需要跳转到https://openapi.alipay.com/gateway.do? + order_string
        # 电脑网站支付(开发环境)，需要跳转到https://openapi.alipaydev.com/gateway.do? + order_string
        alipay_url = settings.ALIPAY_URL + '?' + order_string
        # print(alipay_url)
        return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK', 'alipay_url': alipay_url})
